package com.chance.cc.crawler.development.command.job.domain.weibo;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/1/26 16:25
 * @Description
 *      微博api接口
 **/
public class WeiboApiCrawlerScheduleJob {
    private static final String domainId = "weibo";

    private static final String access_token = "2.00o4_w1HrAaeYBedf38e38b8SnITmD";
    private static final String app_key = "1428199813";

    private static final String subscribeUrl = "https://c.api.weibo.com/subscribe/update_subscribe.json";
    private static final String subscribeSite = "apiCornSubscribe";

    private static final String subscribeDataUrl = "https://c.api.weibo.com/commercial/push?source=1428199813&access_token=2.00o4_w1HrAaeYBedf38e38b8SnITmD&subid=12054";
    private static final String subscribeDataSite = "apiCornSubscribeData";

    private static final String limitedDataUrl = "https://c.api.weibo.com/2/search/statuses/limited.json";
    private static final String limitedDataSite = "apiCornLimitedData";

    private static String service_ip = "192.168.1.215";
    private static int service_port = 9599;


    public static CrawlerJob publishApiSubscribeDataCrawlerScheduleJobInfo(){
        CrawlerJob weiboJob = subscribeDataRecord(domainId);

        //发布单次任务
        CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(service_ip,service_port);
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(weiboJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("微博订阅任务定时采集");
        crawlerScheduleJob.setCrawlerKey(weiboJob.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return weiboJob;

    }

    public static CrawlerJob publishApiSubscribeCrawlerScheduleJobInfo(){
        CrawlerJob weiboJob = subscribeRecord(domainId);

        //发布单次任务
        CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(service_ip,service_port);
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(weiboJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("微博订阅任务关键词");
        crawlerScheduleJob.setCrawlerKey(weiboJob.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return weiboJob;

    }

    public static CrawlerJob publishApiLimitedCrawlerScheduleJobInfo(){
        CrawlerJob weiboJob = limitedDataRecord(domainId);

        //发布单次任务
        CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(service_ip,service_port);
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(weiboJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("微博采集最新微博数据");
        crawlerScheduleJob.setCrawlerKey(weiboJob.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return weiboJob;

    }

    private static CrawlerJob limitedDataRecord(String domainId){
        CrawlerRequestRecord requestRecord = requestRecord(limitedDataUrl, limitedDataSite);
        requestRecord.getHttpRequest().setMethod(HttpConstant.Method.GET);
        CrawlerRequestRecord keywordRecord = keywordRecord(limitedDataSite);


        return CrawlerJob.builder()
                .crawlerJobThreadNumber(3)
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",limitedDataSite,ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domainId,limitedDataSite,"queue")))
                .fileResultPipeline(null, "/data/chance_crawler_test/data/weibo/weiboSubscribe.log", false)
                .requestRecord(requestRecord)
                .supportRecord(keywordRecord)
                .build();

    }

    private static CrawlerJob subscribeDataRecord(String domainId){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl(subscribeDataUrl)
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .build();
        requestRecord.tagsCreator().bizTags().addSite(subscribeDataSite);

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(3)
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",subscribeDataSite,ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domainId,subscribeSite,"queue")))
                .fileResultPipeline(null, "/data/chance_crawler_test/data/weibo/weiboSubscribe.log", false)
                .requestRecord(requestRecord)
                .build();
    }

    private static CrawlerJob subscribeRecord(String domainId){
        CrawlerRequestRecord requestRecord = requestRecord(subscribeUrl, subscribeSite);
        requestRecord.getHttpRequest().addExtra("method", HttpConstant.Method.POST);
        CrawlerRequestRecord keywordRecord = keywordRecord(subscribeSite);

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(3)
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",subscribeSite,ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domainId,subscribeSite,"queue")))
                .fileResultPipeline(null, "/data/chance_crawler_test/data/weibo/weiboSubscribeData.log", false)
                .requestRecord(requestRecord)
                .supportRecord(keywordRecord)
                .build();
    }

    private static CrawlerRequestRecord requestRecord(String url,String site){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl(url)
                .needParsed(false)
                .needWashed(false)
                .releaseTime(System.currentTimeMillis())
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addSite(site);
        return requestRecord;
    }

    private static CrawlerRequestRecord keywordRecord(String site){
        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_search_kw",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domainId+"/keys?site=" + site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return keywordRecord;
    }


}
